from django.shortcuts import render, render_to_response
from django.http import HttpResponse, HttpResponseRedirect
from django.db import connection

def login(request):
	if 'login' in request.POST and 'password' in request.POST:
		cursor = connection.cursor()
		query = "select count(*) from operators where login='%s' and password='%s'"%(request.POST['login'], request.POST['password'])
		cursor.execute(query)
		result = cursor.fetchone()[0]
		print result
		if result == 1:
			request.session['ownerUserLogin'] = True
			request.session['operatorLogin'] = request.POST['login']
		else: 
			return render_to_response('main.html', {'pageType'	:	'loginPage',
													'hasError'	:	u'wrong login'})
			
	return render_to_response('main.html', {'pageType'	:	'loginPage'})

def logout(request):
	if 'ownerUserLogin' in request.session:
		del request.session['ownerUserLogin']
	return HttpResponseRedirect("/")